import axios from 'axios'
import {
  reqAddress,
  reqFoodCategorys,
  reqShops,
  reqUserInfo,
  reqLogout,
  reqSearchShop
} from '../api'

export default {
  namespaced: true,
  state: {
    latitude: 40.10038, // 纬度
    longitude: 116.36867, // 经度
    address: {}, //地址相关信息对象
    categorys: [], // 食品分类数组
    shops: [], // 商家数组
    userInfo: {}, // 用户信息
    goods: [], // 商品列表
    ratings: [], // 商家评价列表
    info: {}, // 商家信息
    cartFoods: [], // 购物车中食物的列表
    searchShops: [], // 搜索得到的商家列表
  },
  //========================================================
  actions: {
    // 异步获取地址
    async getAddress (content) {
      const geohash = content.state.latitude + ',' + content.state.longitude
      const result = await reqAddress(geohash)
      if (result.code === 0){
        const address = result.data
        content.commit('RECEIVE_ADDRESS', address)
      }
    },
    // 异步获取食品分类列表
    async getCategorys(content) {
      // 发送异步ajax请求
      const result = await reqFoodCategorys()
      // 提交一个mutation
      if (result.code === 0) {
        const categorys = result.data
        content.commit('RECEIVE_CATEGORYS', categorys)
      }
    },
    // 异步获取商家列表
    async getShops(content) {
      // 发送异步ajax请求
      const {longitude, latitude} = content.state
      const result = await reqShops(longitude, latitude)
      // 提交一个mutation
      if (result.code === 0) {
        const shops = result.data
        content.commit('RECEIVE_SHOPS', shops)
      }
    },
  },
  //========================================================
  mutations: {
    RECEIVE_ADDRESS(state, address){
      state.address = address
    },
    RECEIVE_CATEGORYS(state, categorys){
      state.categorys = categorys
    },
    RECEIVE_SHOPS(state, shops){
      state.shops = shops
    }
  }
}
